//
//  JSONBaseDeal.m
//  YZTZ
//
//  Created by wangminhong on 14/12/16.
//  Copyright (c) 2014年 cqjr. All rights reserved.
//

#import "JSONBaseDeal.h"

@implementation JSONBaseDeal

#pragma mark - 获得值方法:钥匙参数:数据参数
+ (NSString*)getValue:(NSString*)key fromData:(id)data
{
    if ([[data class] isSubclassOfClass:[NSNull class]])
    {
        return @"";
    }
    else
    {
        id value = [data objectForKey:key];
        if ([[value class] isSubclassOfClass:[NSString class]])
        {
            
            if ([value isEqualToString:@"null"]) {
                
                return @"";
            }
            
            return [NSString stringWithFormat:@"%@", value];
        }
        else if ([[value class] isSubclassOfClass:[NSNull class]])
        {
            return @"";
        }
        else if (value == nil)
        {
            return @"";
        }
        else
        {
            return [NSString stringWithFormat:@"%@", value];
        }
        return [NSString stringWithFormat:@"%@", value];
    }
}

#pragma mark - 获得布尔值方法:钥匙参数:数据参数
+ (BOOL)getBoolValue:(NSString*)key fromData:(id)data
{
    if ([[data class] isSubclassOfClass:[NSNull class]])
    {
        return 0;
    }
    
    id value = [data objectForKey:key];
    
    if ([[value class] isSubclassOfClass:[NSNull class]] || value == nil) {
        
        return false;
    }
    
    if ([[value class] isSubclassOfClass:[NSString class]]) {
        
        NSString *string = ((NSString *)value).lowercaseString;
        if (string.integerValue == 1) {
            
            return true;
        }
        
        return [string isEqualToString:@"true"]?true:false;
    }else{
        
        return [value intValue];
    }
}

#pragma mark - 获得整型值方法:钥匙参数:数据参数
+ (int)getIntValue:(NSString*)key fromData:(id)data
{
    if ([[data class] isSubclassOfClass:[NSNull class]])
    {
        return 0;
    }
    else
    {
        id value = [data objectForKey:key];
        if ([[value class] isSubclassOfClass:[NSNull class]] || value == nil)
        {
            return 0;
        }
        return [value intValue];
    }
}

#pragma mark - 获得整型值方法:钥匙参数:数据参数
+ (double)getDoubleValue:(NSString *)key fromData:(id)data{
    
    if ([[data class] isSubclassOfClass:[NSNull class]]) {
        
        return 0.0;
        
    }else{
        
        id value = [data objectForKey:key];
        
        if ([[value class] isSubclassOfClass:[NSNull class]] || value == nil)
        {
            return 0.0;
        }
        return [value doubleValue];
    }
}

#pragma mark - 获得数据值方法:钥匙参数:数据参数
+ (NSDate *)getDateValue:(NSString *)key fromData:(id)data
{
    if (![data objectForKey:key]) {
        
        return nil;
    }
    
    if ([[[data objectForKey:key] class] isSubclassOfClass:[NSNull class]])
    {
        return nil;
    }
    
    return [NSDate dateWithTimeIntervalSince1970:[[data objectForKey:key] doubleValue]/1000];
}


#pragma mark - 获得对象值方法:钥匙参数:数据参数
+ (id)getObjectValue:(NSString *)key fromData:(id)data
{
    NSString *gender = [self getValue:key fromData:data];
    if ([gender isEqualToString:@"M"])
    {
        return @"男";
    }
    else
    {
        return @"女";
    }
}

#pragma mark - 获得性别值方法:钥匙参数:数据参数
+ (NSString *)getGenderValue:(NSString *)key fromData:(id)data
{
    if ([[data class] isSubclassOfClass:[NSNull class]])
    {
        return nil;
    }
    else
    {
        if (![[[data objectForKey:key] class] isSubclassOfClass:[NSString class]])
        {
            return [data objectForKey:key];
        }
        else
        {
            if ([[data objectForKey:key] isEqualToString:@""])
            {
                return nil;
            }
            else
            {
                return [data objectForKey:key];
            }
        }
    }
}

#pragma mark - 获得数值方法:钥匙参数:数据参数
+ (NSDecimalNumber *)getDecimalValue:(NSString *)key fromData:(id)data{
    
    if ([[data class] isSubclassOfClass:[NSNull class]])
    {
        return [NSDecimalNumber zero];
    }
    else
    {
        id value = [data objectForKey:key];
        
        if ([[value class] isSubclassOfClass:[NSNull class]] || value == nil)
        {
            return [NSDecimalNumber zero];
            
        }else if([[value class] isSubclassOfClass:[NSNumber class]]){
            
            return [[NSDecimalNumber alloc] initWithDecimal:[value decimalValue]];
        }
        return [[NSDecimalNumber alloc] initWithString:value];
    }    
}


+ (NSDecimalNumber *)getDecimalValueWithNull:(NSString *)key fromData:(id)data{
    
    if ([[data class] isSubclassOfClass:[NSNull class]])
    {
        return nil;
    }
    else
    {
        id value = [data objectForKey:key];
        
        if ([[value class] isSubclassOfClass:[NSNull class]] || value == nil)
        {
            return nil;
            
        }else if([[value class] isSubclassOfClass:[NSNumber class]]){
            
            return [[NSDecimalNumber alloc] initWithDecimal:[value decimalValue]];
        }else if([[value class] isSubclassOfClass:[NSString class]]){
            
            if ([(NSString *)value length] == 0) {
                
                return nil;
            }
        }
        
        return [[NSDecimalNumber alloc] initWithString:value];
    }
}

+ (NSNumber *)getNumberValue:(NSString *)key fromData:(id)data{
    
    if ([[data class] isSubclassOfClass:[NSNull class]])
    {
        return [NSNumber numberWithInt:0];
    }
    else
    {
        id value = [data objectForKey:key];
        
        if ([[value class] isSubclassOfClass:[NSNull class]] || value == nil)
        {
            return [NSNumber numberWithInt:0];
            
        }else if([[value class] isSubclassOfClass:[NSNumber class]]){
            
            return value;
        }
        
        NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
        return [f numberFromString:value];
        
    }
}

@end
